package com.moorecat.app;

import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;
import com.moorecat.bean.Coupon;
import com.moorecat.bean.Promotion;
import com.moorecat.bean.User;
import com.moorecat.exception.SVException;
import com.moorecat.http.HttpUtils;

/**
 * Created by IntelliJ IDEA.
 * User: yuml
 * Date: 11-7-31
 * Time: 下午8:48
 *  发送优惠短信
 */
public class BusinessCouponSMSTask extends AsyncTask<String,Integer,String>{
    private User user;
    private Coupon coupon;
    private BusinessInfo ctx;
    private boolean isCoupon;
    private Promotion promotion;

    public BusinessCouponSMSTask(BusinessInfo ctx, User user, Coupon coupon) {
        this.user = user;
        this.coupon = coupon;
        this.ctx = ctx;
        this.isCoupon = true;
    }

    public BusinessCouponSMSTask(BusinessInfo ctx, User user, Promotion promotion) {
        this.user = user;
        this.promotion = promotion;
        this.ctx = ctx;
        this.isCoupon = false;
    }

    @Override
    protected String doInBackground(String... strings) {
        String rs = null;
        try{
            ctx.setDownloading(true);
            if(user!=null && user.getPhone() !=null){
                if(isCoupon){
                    if(coupon!=null && coupon.getId() > 0){
                        rs = HttpUtils.sendSPSMS(coupon.getId(), user.getPhone(), true);
                    }
                }else{
                    if(promotion!=null && promotion.getId() > 0){
                        rs = HttpUtils.sendSPSMS(promotion.getId(), user.getPhone(), false);
                    }
                }

            }
        }catch (SVException e){
            rs = e.getMessage();
        }
        return rs;
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        Toast.makeText(ctx, s,Toast.LENGTH_LONG).show();
        ctx.setDownloading(false);
    }
}
